package com.probiz.estore.customer.service.impl;

import java.util.List;

import com.probiz.estore.attribute.dao.CustomerAttrValueDao;
import com.probiz.estore.common.model.attribute.AttributeValue;
import com.probiz.estore.common.model.attribute.CustomerAttrValue;
import com.probiz.estore.common.model.customer.Customer;
import com.probiz.estore.common.service.AttributeService;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.customer.service.CustomerAttrValueManager;


public class CustomerAttrValueManagerImpl extends GenericManagerImpl<CustomerAttrValue> implements CustomerAttrValueManager {

	private CustomerAttrValueDao customerAttrValueDao = null;
	private AttributeService attributeService = null;
	
	public void setCustomerAttrValueDao(CustomerAttrValueDao customerAttrValueDao) {
		this.customerAttrValueDao = customerAttrValueDao;
	}

	public AttributeService getAttributeService() {
		return attributeService;
	}

	public void setAttributeService(AttributeService attributeService) {
		this.attributeService = attributeService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = customerAttrValueDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(CustomerAttrValue entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onSave(java.lang.Object)
	 */
	@Override
	protected void preSave(CustomerAttrValue entity) {

	}

	
	
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.CustomerAttrValueManager#saveCustomerAttrValueFromRequest(com.probiz.estore.common.model.customer.Customer, java.util.List)
	 */
	public void saveCustomerAttrValueFromRequest(Customer customer,List<AttributeValue> attributeValues){
		
		for (AttributeValue attributeValue : attributeValues) {
			CustomerAttrValue customerAttrValue = (CustomerAttrValue) attributeValue;
			if (customerAttrValue.getId() == null) {
				//start:判断是否“真”新增
				CustomerAttrValue existCustomerAttrValue = customerAttrValueDao.getCustomerAttrValue(customer.getAppuserId(),customerAttrValue.getAttributeId());
				//end:判断是否“真”新增
				if(existCustomerAttrValue == null){
					//case:真“新增”
				}else{
					//case:假“新增”：出现假新增的原因在于后台save是用ajax提交，在属性第一次保存后，新增的id并没有set回页面中(！！目前全部为假真增)
					this.evict(existCustomerAttrValue);
					customerAttrValue.setCustomerAttrValueId(existCustomerAttrValue.getCustomerAttrValueId());
				}
			}
		}
		// 保存、更新自定义属性
		attributeService.saveOrUpdateAttributeValue(attributeValues, customer);
	}
}
